package com.twu.biblioteca;

import java.util.*;

public class Runner {
    ArrayList<String> commands = new ArrayList<String>(
            Arrays.asList("List All Books", "List Available Books", "Checkout Book",
                    "Return Book", "Exit"));
    HashSet<String> allBooks = new HashSet<String>(
            Arrays.asList("Book1", "Book2", "Book3"));
    HashSet<String> checkedOutBooks = new HashSet<String>(
            Arrays.asList("Book3"));


    public Runner() {
        this.welcome();
        this.displayMenu();
    }
    public void welcome() {
        System.out.println("Welcome to Biblioteca");
    }

    public void displayMenu() {
        System.out.println("\nMain Menu.  Choose from the following choices:");
        for(String command:commands){
            System.out.println(command);
        }
        String response = getResponse();
        applyResponse(response);
        }



    private String getResponse() {
        Scanner scanner = new Scanner(System.in);
        return scanner.nextLine();
    }

    private void applyResponse(String response) {
        if (response.equals("List All Books")) {
            listAllBooks();
        }

        else if (response.equals("List Available Books")) {
            listAvailableBooks();
        }

        else if (response.equals("Checkout Book")) {
            checkoutBook();
        }

        else if (response.equals("Return Book")){
            returnBook();
        }

        else if (response.equals("Exit")) {
            System.exit(0);
        }
        else {
            System.out.println("Select a valid option!");
            displayMenu();

    }
        displayMenu();
}
    private void listAllBooks() {
        for (String book:allBooks){
            System.out.println(book);
        }
    }

    private void listAvailableBooks() {
        HashSet<String> availableBooks = getAvailableBooks();
        Iterator iterator = availableBooks.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }

    private void checkoutBook() {
        System.out.println("Choose a book");

        String book = getResponse();
        HashSet<String> availableBooks = getAvailableBooks();
        if (availableBooks.contains(book)) {
            checkedOutBooks.add(book);
            System.out.println("Thank you! Enjoy the book");
        }
        else {
            System.out.println("That book is not available.");
            displayMenu();
        }

    }
    private void returnBook() {
        System.out.println("Which book are you returning?");
        String book = getResponse();
        HashSet<String> availableBooks = getAvailableBooks();
        if (checkedOutBooks.contains(book)) {
            checkedOutBooks.remove((book));
            System.out.println("Thank you for returning the book.");
            }
        else {
            System.out.println("That is not a valid book to return.");
            displayMenu();
        }
    }

    private HashSet<String> getAvailableBooks() {
        HashSet<String> availableBooks = new HashSet<String>();
        for (String book:allBooks){
            if (checkedOutBooks.contains(book)) {
                ;
            }
            else {
                    availableBooks.add(book);
            }
        }
        return availableBooks;
    }

}



